package com.miao.teacher.utils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import android.net.ParseException;

public class ConvertUtils {

	public static byte[] GetBytes(Object[] recordingArray) {
		return ListByte2ByteArray(ObjectArray2ListByte(recordingArray));
	}

	public static byte[] GetBytes2(Object[] recordingArray) {
		return ListByte2ByteArray(ObjectArray2ListByte2(recordingArray));
	}

	public static List<byte[]> ObjectArray2ListByte2(Object[] recordingArray) {
		List<byte[]> wavDatas = new ArrayList<byte[]>();
		for (int i = 0; i < recordingArray.length; i++) {
			byte[] recordingData = (byte[]) recordingArray[i];
			if (recordingData != null) {
				wavDatas.add(recordingData);
			}
		}
		return wavDatas;
	}

	public static List<byte[]> ObjectArray2ListByte(Object[] recordingArray) {
		List<byte[]> wavDatas = new ArrayList<byte[]>();
		short[] buffer;
		for (int i = 0; i < recordingArray.length; i++) {
			short[] recordingData = (short[]) recordingArray[i];
			if (recordingData != null) {
				byte[] bytes = ConvertUtils.shortToByteSmall(recordingData);
				wavDatas.add(bytes);
			}
		}
		return wavDatas;
	}

	public static byte[] ListByte2ByteArray(List<byte[]> mList) {
		byte[] mRes = null;
		try {
			int fileLength = 0;
			for (byte[] bytes : mList) {
				fileLength += bytes.length;
			}
			mRes = new byte[fileLength];
			int y = 0;
			for (byte[] bytes : mList) {
				for (int x = 0; x < bytes.length; x++) {
					mRes[y] = bytes[x];
					y++;
				}
			}
		} catch (Exception er) {
		}
		return mRes;
	}

	public short[] byteArray2ShortArray(byte[] data, int items) {
		short[] retVal = new short[items];
		for (int i = 0; i < retVal.length; i++)
			retVal[i] = (short) ((data[i * 2] & 0xff) | (data[i * 2 + 1] & 0xff) << 8);
		return retVal;
	}

	public static byte[] shortToByteSmall(short[] buf) {

		byte[] bytes = new byte[buf.length * 2];
		for (int i = 0, j = 0; i < buf.length; i++, j += 2) {
			short s = buf[i];

			byte b1 = (byte) (s & 0xff);
			byte b0 = (byte) ((s >> 8) & 0xff);

			bytes[j] = b1;
			bytes[j + 1] = b0;
		}
		return bytes;

	}

	/*
	 * 获取当前时间 时间格式yyyy-mm-dd
	 */
	public static String getCurTime() {
		SimpleDateFormat sDateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		String date = sDateFormat.format(new java.util.Date());
		return date;
	}

	/** */
	/**
	 * 文件转化为字节数组
	 */
	public static byte[] getBytesFromFile(File f) {
		if (f == null) {
			return null;
		}
		try {
			FileInputStream stream = new FileInputStream(f);
			ByteArrayOutputStream out = new ByteArrayOutputStream(1000);
			byte[] b = new byte[1000];
			int n;
			while ((n = stream.read(b)) != -1)
				out.write(b, 0, n);
			stream.close();
			out.close();
			return out.toByteArray();
		} catch (IOException e) {
		}
		return null;
	}
	public static String getFTime(String time)
	{
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try{
			java.util.Date date = df.parse(time);
			return (date.getHours()>9?date.getHours()+"":"0"+date.getHours())+":"
					+(date.getMinutes()>9?date.getMinutes()+"":"0"+date.getMinutes())+" "
					+(date.getMonth()>9?date.getMonth()+"":"0"+date.getMonth())+"-"
					+(date.getDate()>9?date.getDate()+"":"0"+date.getDate());
		}catch(Exception er)
		{}
		return "";
	}
	public static String getWebTime(String time) {
		StringBuffer sb = new StringBuffer();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		java.util.Date now;
		try {
			now = df.parse(getCurTime());
			java.util.Date date = df.parse(time);
			long l = now.getTime() - date.getTime();
			long day = l / (24 * 60 * 60 * 1000);
			long hour = (l / (60 * 60 * 1000) - day * 24);
			long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
			long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);

			if (day > 0) {
				if (day == 1) {
					sb.append(day + "天前");
				} else {
					sb.append(day + "天前");
				}
			} else if (hour > 0) {
				if (hour == 1) {
					sb.append(hour + "小时前");
				} else {
					sb.append(hour + "小时前");
				}
			} else if (min > 0) {
				if (min < 3) {
					sb.append("刚刚");
				} else {
					sb.append(min + "分钟前");
				}
			} else {
				sb.append("刚刚");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sb.toString();
	}
}
